const { exec } = require('../db/index')
class BlogService {
  async add(ctx) {
    const { title, content, author } = ctx.request.body;
    const data = await exec(`insert into blogs (title, content, author) values (?,?,?)`, [title, content, author])
    return {
      code: 0,
      msg: 'success',
      data
    }
  }
  // search
  async search(ctx) {
    const { key } = ctx.query;
    const data = await exec(`select * from blogs where title like '%${key}%'`)
    return {
      code: 0,
      msg: 'success',
      data
    }
  }
  // update
  async update(ctx) {
    const id = ctx.params.id
    const { title, content, author } = ctx.request.body;
    const data = await exec(`update blogs set title=?, content=?, author=? where id=?`, [title, content, author, id])
    return {
      code: 0,
      msg: 'success',
      data
    }

  }
}

module.exports = new BlogService